package Client;
import javax.swing.*;

import Email.Email;
import Entities.Voter;
import Helpers.DBMethods;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

//public class registerPage implements ActionListener{
//	JFrame Rframe;
//	Boardc rpanel;
//	JFrame mframe;
//	public registerPage(JFrame mframe){
//		this.mframe = mframe;
//		mframe.setVisible(false);
//		Rframe = new JFrame("Online Voting System!");
//		Rframe.setSize(600,600);
//		Rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		Rframe.setResizable(true);
//		rpanel = new Boardc();
//		Rframe.setContentPane(rpanel);	
//
//		JButton back = new JButton("Back");
//		back.setBounds(300, 230, 85, 30);
//		back.addActionListener(this);
//		back.setActionCommand("back");
//		rpanel.add(back);
//		back.setVisible(true);
//		Rframe.setVisible(false);
//	}
//
//	public void setV3(){
//		Rframe.setVisible(true);
//	}
//
//	public void actionPerformed(ActionEvent e){
//		if(e.getActionCommand().equals("back")){
//			this.setV2();
//			mframe.setVisible(true);
//		}
//	}
//	private void setV2() {
//		Rframe.setVisible(false);
//	}
//}
@SuppressWarnings("serial")
class Boardc extends JPanel implements ActionListener{	
	JLabel registerLabel, name, emailLabel, erremail, sucess, captchaError;
	JButton submit;
	JTextField emailField;
	String email;
	OurCaptcha c;
	JFrame mframe;
	JButton back = new JButton("Back");
	public Boardc(JFrame mframe){
		back.setBounds(300, 230, 85, 30);
		back.addActionListener(this);
		back.setActionCommand("back");
		add(back);
		this.mframe = mframe;
		//by Karim for CAPTCHA
		c = new OurCaptcha();
		c.setBounds(50, 350, 200, 160);
		add(c);

		//by Adam
		setLayout(null); 	
		setBackground(Color.WHITE);
		registerLabel = new JLabel("Register to start voting:");
		name = new JLabel("Please enter your email address and your password will be sent to it:");
		emailLabel = new JLabel("Email address:");
		sucess = new JLabel("Please check your inbox for your password");
		erremail = new JLabel("Please enter a valid email address");
		captchaError = new JLabel("Please retry entering the Capthca code");
		submit = new JButton("Submit");
		emailField = new JTextField();

		registerLabel.setBounds(40,40,400,30);		// x/y/width/length
		emailField.setBounds(140, 130, 240, 30);
		submit.setBounds(40, 230, 85, 30);
		name.setBounds(40,80,500,30);		
		emailLabel.setBounds(40,130,150,30);		
		erremail.setBounds(220,270,300,30);		
		sucess.setBounds(220,290,300,30);
		captchaError.setBounds(250, 480, 300, 30);

		submit.addActionListener(this);

		registerLabel.setFont(new Font("Dialog", Font.PLAIN, 24));
		erremail.setForeground(Color.RED);
		captchaError.setForeground(Color.RED);

		add(registerLabel);   
		add(submit);
		add(name);   
		add(emailLabel);
		add(erremail);
		add(sucess);
		add(emailField);
		add(captchaError);

		captchaError.setVisible(false);
		emailField.setVisible(true);
		name.setVisible(true);
		erremail.setVisible(false);
		sucess.setVisible(false);
		emailLabel.setVisible(true);
		submit.setVisible(true);
		registerLabel.setVisible(true);
	}

	public void actionPerformed(ActionEvent e){	
		if(e.getActionCommand().equals("Submit")){	
			email = emailField.getText();
			if(email.isEmpty() || !email.contains("@") || !email.contains("eg") || !email.contains(".") || email.length()<8)				
				erremail.setVisible(true);
			else if(!c.validateAnswer(c.getText()))
				captchaError.setVisible(true);
			else if(!email.isEmpty() && email.contains("@") && email.contains("eg") && email.contains(".") && email.length()>=8){
				{
					Voter v = new DBMethods().getStudentByEmail(email);
					if(v!=null)
					{
						Random r = new Random();
						String password = Long.toString(Math.abs(r.nextInt()),36);
						new DBMethods().updateStudentByEmail(email, password,1);
						new Email().sendRegPassword(v.getId(), v.getEmail(), password);
						mframe.getContentPane().removeAll();
						mframe.repaint();
						JPanel p = new Board(mframe);
						p.setBounds(0, 0, 600, 600);
						mframe.add(p);//.setV3();
						mframe.validate();
						mframe.repaint();
					}
					else
					{
						System.out.println("Email doesnot exists");
					}
				}
					
			}
		}else if(e.getActionCommand().equals("back")){
			mframe.getContentPane().removeAll();
			mframe.repaint();
			JPanel p = new Board(mframe);
			p.setBounds(0, 0, 600, 600);
			mframe.add(p);//.setV3();
			mframe.validate();
			mframe.repaint();
		}else{
			System.out.println("problem!!");
		}
	}
}
